home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / gsdbloo.exe / GS_MAKMO.PAS < prev    next >
Pascal/Delphi Source File  |  1992-02-24  |  5KB  |  179 lines

  1. unit GS_MakMo;
  2. {------------------------------------------------------------------------------
  3.                             DBase Memo File Builder
  4.  
  5.        Copyright (c)  Richard F. Griffin
  6.  
  7.        20 February 1992
  8.  
  9.        102 Molded Stone Pl
  10.        Warner Robins, GA  31088
  11.  
  12.        -------------------------------------------------------------
  13.        This unit creates a test dBase memo record.
  14.  
  15.        This unit consists of the unit GS_MakeMemo.  This will initialize
  16.        a work buffer, find the next available memo file block, store items
  17.        in an assigned memory buffer, and write the memo to the memo file
  18.        upon command.
  19.  
  20.        The object is initialized as follows:
  21.  
  22.           Init(m, siz);
  23.  
  24.           where:
  25.                   m     = dbase file object pointer of type GSP_dBFld_Objt.
  26.                           this must be assigned by the calling program.
  27.                           Init assuumes there was a memo field in the
  28.                           .DBF file the calling program initialized.  If
  29.                           a memo field is not in the dBase file, strange
  30.                           things will happen.
  31.  
  32.                   siz   = Size of memory buffer to assign.  Must be greater
  33.                           than the maximum memo field to be created.
  34.  
  35.        The memo filed is initialized as follows:
  36.  
  37.           ResetMemoData
  38.  
  39.                            This resets the buffer to a zero contents count
  40.                            and fills the buffer with zeros.
  41.  
  42.        The memo data is added as follows:
  43.  
  44.           InsertMemoData(st);
  45.  
  46.           where:
  47.                   st    = string to be stored in the buffer.  If a CR/LF is
  48.                           desired, they must be added by the caller.  For
  49.                           example: st := st+#13#10.
  50.  
  51.        The memo data is written to the file as follows:
  52.  
  53.           st := WriteMemoRecord;
  54.  
  55.           where:
  56.                   st    = string will contain the memo block number in the
  57.                           format for storage in the dBase.DBF memo field.
  58.                           Typically, this function would be called as:
  59.  
  60.                           FieldPut(memofield, memoobject.WriteMemoRecord);
  61.  
  62. -------------------------------------------------------------------------------}
  63.  
  64. interface
  65. {$D-}
  66.  
  67. uses
  68.    CRT,
  69.    DOS,
  70.    GS_Strng,
  71.    GS_dBase,
  72.    GS_FileH,
  73.    GS_dBFld;
  74.  
  75. type
  76.    MemoBlockPntr = ^MemoBlockArry;
  77.    MemoBlockArry = array[0..MaxInt] of byte;
  78.  
  79.    GS_MakeMemoP = ^GS_MakeMemo;
  80.    GS_MakeMemo = object
  81.       mCnt,
  82.       tcnt  :  longint;              {Counter for input buffer char position}
  83.       Result  : word;                {BlockWrite number of bytes written}
  84.       Mem_Block : MemoBlockPntr;
  85.       Memo_Loc  : longint;
  86.       Memo_Size : integer;
  87.       ml  : GSP_dBFld_Objt;
  88.       fli : text;
  89.       constructor Init(m : GSP_dBFld_Objt; siz : integer);
  90.       destructor Done;
  91.       procedure InsertMemoData(st : string);
  92.       procedure ResetMemoData;
  93.       function WriteMemoRecord : string;
  94.    end;
  95. implementation
  96. const
  97.    EOFMark : byte = $1A;              {End of disk file code}
  98.  
  99. var
  100.    valu  : string[10];                {work string to convert block number}
  101.    n,
  102.    s,
  103.    w   : string;
  104.    r   : longint;
  105.    i   : integer;
  106.    d   : integer;
  107.  
  108. constructor GS_MakeMemo.Init(m : GSP_dBFld_Objt; siz : integer);
  109. BEGIN
  110.    ml := m;
  111.    Memo_Size := siz;
  112.    GetMem(Mem_Block, Memo_Size);
  113.    GS_FileRead(ml^.mFile, 0, Mem_Block^, 1, Result);
  114.                                       {read a block from the .DBT}
  115.    Move(Mem_Block^[0],Memo_Loc,4);     {Get next block number to append}
  116.    tcnt := Memo_Loc;
  117.    mcnt := 0;
  118.    FillChar(Mem_Block^,Memo_Size,#0);
  119. end;
  120.  
  121. destructor GS_MakeMemo.Done;
  122. begin
  123.    if Mem_Block <> nil then FreeMem(Mem_Block, Memo_Size);
  124.    Mem_Block := nil;
  125. end;
  126.  
  127. procedure GS_MakeMemo.InsertMemoData(st : string);
  128. begin
  129.    move(st[1],Mem_Block^[mCnt],length(st));
  130.    mCnt := mCnt + length(st);
  131. end;
  132.  
  133. procedure GS_MakeMemo.ResetMemoData;
  134. BEGIN
  135.    GS_FileRead(ml^.mFile, 0, Mem_Block^, 1, Result);
  136.                                       {read a block from the .DBT}
  137.    Move(Mem_Block^[0],Memo_Loc,4);     {Get next block number to append}
  138.    tcnt := Memo_Loc;
  139.    mcnt := 0;
  140.    FillChar(Mem_Block^,Memo_Size,#0);
  141. end;
  142.  
  143.  
  144. function GS_MakeMemo.WriteMemoRecord : string;
  145. var
  146.    loc : integer;
  147.    li  : longint;
  148. begin
  149.    if mCnt = 0 then
  150.    begin
  151.       WriteMemoRecord := '          ';
  152.       exit;
  153.    end;
  154.    Mem_Block^[mCnt] := EOFMark;
  155.    inc(mCnt);
  156.    loc := 0;
  157.    while (mCnt > GS_dBase_MaxMemoRec) do
  158.    begin
  159.       GS_FileWrite(ml^.mFile,tcnt,Mem_Block^[loc],1, Result);
  160.                                                  {write a block to the .DBT}
  161.       inc(tcnt);
  162.       loc := loc + GS_dBase_MaxMemoRec;
  163.       mCnt := mCnt - GS_dBase_MaxMemoRec;
  164.    end;
  165.    GS_FileWrite(ml^.mFile,tcnt,Mem_Block^[loc],1, Result);
  166.                                       {Write the last block to the .DBT}
  167.    FillChar(Mem_Block^,GS_dBase_MaxMemoRec,#0);
  168.    li := GS_FileSize(ml^.mFile);
  169.    Move(li,Mem_Block^[0],4);
  170.    GS_FileWrite(ml^.mFile,0,Mem_Block^,1, Result);
  171.                                       {Write the block to the .DBT.  It will}
  172.                                       {point to the next available block};
  173.    str(Memo_Loc:10,valu);
  174.    WriteMemoRecord := valu;
  175. end;
  176.  
  177. end.
  178.  
  179.